<?php
require_once __DIR__ . '/../vendor/autoload.php';
use GatewayWorker\BusinessWorker;
use GatewayWorker\Gateway;
use GatewayWorker\Register;
use Workerman\Worker;

class Start{

    public function __construct(){
        global $argv;
        //兼容windows启动方式
        call_user_func([$this,$argv[2]]);
        //以守护进程的方式启动
        if(isset($argv[3])){
            $argv[2] = '-d';
        }
        Worker::runAll();
    }

    private function business()
    {
        $worker                  = new BusinessWorker();
        $worker->name            = 'BusinessWorker'.mt_rand(1000,99999);                        #设置BusinessWorker进程的名称
        $worker->count           = 1;                                       #设置BusinessWorker进程的数量
        $worker->registerAddress = '127.0.0.1:9554';                        #注册服务地址
    }

    private function gateway()
    {
        $gateway = new Gateway("websocket://0.0.0.0:9553");
        $gateway->name                 = 'Gateway';                         #设置Gateway进程的名称，方便status命令中查看统计
        $gateway->count                = 1;                                    #进程的数量
        $gateway->lanIp                = '127.0.0.1';                       #内网ip,多服务器分布式部署的时候需要填写真实的内网ip
        $gateway->startPort            = 9555;                              #监听本机端口的起始端口

        // pingInterval*pingNotResponseLimit 秒内没有任何请求则断开连接 心跳检测
        $gateway->pingInterval         = 55;
        $gateway->pingNotResponseLimit = 1;

        $gateway->registerAddress      = '127.0.0.1:9554';                  #注册服务地址
    }

    private function register()
    {
        new Register('text://127.0.0.1:9554');
    }

    private function all(){
        $this->register();
        $this->gateway();
        $this->business();
    }
}

new Start();
